/*
* Copyright 2016 YONYOU Corporation Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project         marketing

* @Package         com.yonyou.marketing.message.push.resource

* @Author          LuZhen

* @FileName        MessageProtocolHandleInfo

* @Date            2016/11/15

----------------------------------------------------------------------------------
*     Date          Who             Version         Comments
* 1. 2016/11/15        LuZhen          1.0             Create Source
*
----------------------------------------------------------------------------------
*/
package org.marketing.common.entity.wechatpushmessage;


import org.marketing.common.constants.MessagePushConstant;
import org.marketing.common.util.FtpUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 消息处理信息
 * 由监听器回写
 */
public class WeChatMessageProtocolHandleInfo {

    /**
     * 消息唯一标号
     */
    private String pushId;

    /**
     * 最近一次处理时间
     */
    private Date lastHandleDate;

    /**
     * 最近一次处理人
     */
    private String lastHandler;

    /**
     * 处理次数
     */
    private AtomicInteger handleTimes ;
    /**
     * 处理历史信息的简述信息
     */
    private List<String> handleSimpleHistory;
    /**
     * 是否丢弃
     */
    private boolean isDeprive;

    public boolean isDeprive() {
        return isDeprive;
    }

    public void setDeprive(boolean deprive) {
        isDeprive = deprive;
        this.lastHandleDate = new Date();
        this.lastHandler = FtpUtils.getHostName();
    }

    public String getLastHandler() {
        return lastHandler;
    }

    public String getPushId() {
        return pushId;
    }

    public Date getLastHandleDate() {
        return lastHandleDate;
    }

    public Integer getHandleTimes() {
        return handleTimes.intValue();
    }

    public void addHandleTimes() {
        handleTimes.addAndGet(1);
        this.lastHandleDate = new Date();
        this.lastHandler = FtpUtils.getHostName();
    }

    public List<String> getHandleSimpleHistory() {
        return handleSimpleHistory;
    }

    public void addHandleSimpleHistory(String handleSimpleHistory) {
    	if(this.handleSimpleHistory!=null)
    		this.handleSimpleHistory.add(this.toString()+"@@"+handleSimpleHistory);
    }

    /**
     * 解析消息处理信息
     * 1 - 只有重处理的消息才会自带MessageProtocolHandleInfo信息
     * 2 - 重处理的消息需要判断是否达到最大重试次数
     * 3 - 新消息，需要进行初始化 后续处理流程需要使用到
     * @param handleInfo    消息协议
     * @return MessageProtocolHandleInfo    处理过的消息协议
     */
    public static WeChatMessageProtocolHandleInfo init(WeChatMessageProtocolHandleInfo handleInfo){
        if(handleInfo==null){
            //首次处理
            return new WeChatMessageProtocolHandleInfo();
        }else{
            if(handleInfo.getHandleTimes()>= MessagePushConstant.MESSAGE_MAX_RETRY_TIMES){
                //丢弃
                handleInfo.setDeprive(true);
                return handleInfo;
            }else{
                //增加本次处理信息
                handleInfo.addHandleTimes();
                return handleInfo;
            }
        }
    }


    /**
     * 初始化处理信息
     */
    public WeChatMessageProtocolHandleInfo(){
        handleTimes = new AtomicInteger(1);
        lastHandleDate = new Date();

        pushId = FtpUtils.getUUID();
        lastHandler = FtpUtils.getHostName();

    }

    @Override
    public String toString() {
        return "MessageProtocolHandleInfo{" +
                "pushId='" + pushId + '\'' +
                ", lastHandleDate=" + lastHandleDate +
                ", lastHandler='" + lastHandler + '\'' +
                ", handleTimes=" + handleTimes +
                ", isDeprive=" + isDeprive +
                '}';
    }
}
